/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package hibernatecontrolador;

import hibernatecontrolador.exceptions.IllegalOrphanException;
import hibernatecontrolador.exceptions.NonexistentEntityException;
import hibernatemodelo.Contacto;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import hibernatemodelo.Nota;
import java.util.ArrayList;
import java.util.Collection;
import hibernatemodelo.Seramigo;
import hibernatemodelo.Evento;
import hibernatemodelo.Invitar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Z510
 */
public class ContactoJpaController implements Serializable {

    public ContactoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Contacto contacto) {
        if (contacto.getNotaCollection() == null) {
            contacto.setNotaCollection(new ArrayList<Nota>());
        }
        if (contacto.getSeramigoCollection() == null) {
            contacto.setSeramigoCollection(new ArrayList<Seramigo>());
        }
        if (contacto.getSeramigoCollection1() == null) {
            contacto.setSeramigoCollection1(new ArrayList<Seramigo>());
        }
        if (contacto.getEventoCollection() == null) {
            contacto.setEventoCollection(new ArrayList<Evento>());
        }
        if (contacto.getInvitarCollection() == null) {
            contacto.setInvitarCollection(new ArrayList<Invitar>());
        }
        if (contacto.getInvitarCollection1() == null) {
            contacto.setInvitarCollection1(new ArrayList<Invitar>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Nota> attachedNotaCollection = new ArrayList<Nota>();
            for (Nota notaCollectionNotaToAttach : contacto.getNotaCollection()) {
                notaCollectionNotaToAttach = em.getReference(notaCollectionNotaToAttach.getClass(), notaCollectionNotaToAttach.getId());
                attachedNotaCollection.add(notaCollectionNotaToAttach);
            }
            contacto.setNotaCollection(attachedNotaCollection);
            Collection<Seramigo> attachedSeramigoCollection = new ArrayList<Seramigo>();
            for (Seramigo seramigoCollectionSeramigoToAttach : contacto.getSeramigoCollection()) {
                seramigoCollectionSeramigoToAttach = em.getReference(seramigoCollectionSeramigoToAttach.getClass(), seramigoCollectionSeramigoToAttach.getId());
                attachedSeramigoCollection.add(seramigoCollectionSeramigoToAttach);
            }
            contacto.setSeramigoCollection(attachedSeramigoCollection);
            Collection<Seramigo> attachedSeramigoCollection1 = new ArrayList<Seramigo>();
            for (Seramigo seramigoCollection1SeramigoToAttach : contacto.getSeramigoCollection1()) {
                seramigoCollection1SeramigoToAttach = em.getReference(seramigoCollection1SeramigoToAttach.getClass(), seramigoCollection1SeramigoToAttach.getId());
                attachedSeramigoCollection1.add(seramigoCollection1SeramigoToAttach);
            }
            contacto.setSeramigoCollection1(attachedSeramigoCollection1);
            Collection<Evento> attachedEventoCollection = new ArrayList<Evento>();
            for (Evento eventoCollectionEventoToAttach : contacto.getEventoCollection()) {
                eventoCollectionEventoToAttach = em.getReference(eventoCollectionEventoToAttach.getClass(), eventoCollectionEventoToAttach.getId());
                attachedEventoCollection.add(eventoCollectionEventoToAttach);
            }
            contacto.setEventoCollection(attachedEventoCollection);
            Collection<Invitar> attachedInvitarCollection = new ArrayList<Invitar>();
            for (Invitar invitarCollectionInvitarToAttach : contacto.getInvitarCollection()) {
                invitarCollectionInvitarToAttach = em.getReference(invitarCollectionInvitarToAttach.getClass(), invitarCollectionInvitarToAttach.getId());
                attachedInvitarCollection.add(invitarCollectionInvitarToAttach);
            }
            contacto.setInvitarCollection(attachedInvitarCollection);
            Collection<Invitar> attachedInvitarCollection1 = new ArrayList<Invitar>();
            for (Invitar invitarCollection1InvitarToAttach : contacto.getInvitarCollection1()) {
                invitarCollection1InvitarToAttach = em.getReference(invitarCollection1InvitarToAttach.getClass(), invitarCollection1InvitarToAttach.getId());
                attachedInvitarCollection1.add(invitarCollection1InvitarToAttach);
            }
            contacto.setInvitarCollection1(attachedInvitarCollection1);
            em.persist(contacto);
            for (Nota notaCollectionNota : contacto.getNotaCollection()) {
                Contacto oldIdcreadorOfNotaCollectionNota = notaCollectionNota.getIdcreador();
                notaCollectionNota.setIdcreador(contacto);
                notaCollectionNota = em.merge(notaCollectionNota);
                if (oldIdcreadorOfNotaCollectionNota != null) {
                    oldIdcreadorOfNotaCollectionNota.getNotaCollection().remove(notaCollectionNota);
                    oldIdcreadorOfNotaCollectionNota = em.merge(oldIdcreadorOfNotaCollectionNota);
                }
            }
            for (Seramigo seramigoCollectionSeramigo : contacto.getSeramigoCollection()) {
                Contacto oldIdamigo2OfSeramigoCollectionSeramigo = seramigoCollectionSeramigo.getIdamigo2();
                seramigoCollectionSeramigo.setIdamigo2(contacto);
                seramigoCollectionSeramigo = em.merge(seramigoCollectionSeramigo);
                if (oldIdamigo2OfSeramigoCollectionSeramigo != null) {
                    oldIdamigo2OfSeramigoCollectionSeramigo.getSeramigoCollection().remove(seramigoCollectionSeramigo);
                    oldIdamigo2OfSeramigoCollectionSeramigo = em.merge(oldIdamigo2OfSeramigoCollectionSeramigo);
                }
            }
            for (Seramigo seramigoCollection1Seramigo : contacto.getSeramigoCollection1()) {
                Contacto oldIdamigo1OfSeramigoCollection1Seramigo = seramigoCollection1Seramigo.getIdamigo1();
                seramigoCollection1Seramigo.setIdamigo1(contacto);
                seramigoCollection1Seramigo = em.merge(seramigoCollection1Seramigo);
                if (oldIdamigo1OfSeramigoCollection1Seramigo != null) {
                    oldIdamigo1OfSeramigoCollection1Seramigo.getSeramigoCollection1().remove(seramigoCollection1Seramigo);
                    oldIdamigo1OfSeramigoCollection1Seramigo = em.merge(oldIdamigo1OfSeramigoCollection1Seramigo);
                }
            }
            for (Evento eventoCollectionEvento : contacto.getEventoCollection()) {
                Contacto oldIdcreadorOfEventoCollectionEvento = eventoCollectionEvento.getIdcreador();
                eventoCollectionEvento.setIdcreador(contacto);
                eventoCollectionEvento = em.merge(eventoCollectionEvento);
                if (oldIdcreadorOfEventoCollectionEvento != null) {
                    oldIdcreadorOfEventoCollectionEvento.getEventoCollection().remove(eventoCollectionEvento);
                    oldIdcreadorOfEventoCollectionEvento = em.merge(oldIdcreadorOfEventoCollectionEvento);
                }
            }
            for (Invitar invitarCollectionInvitar : contacto.getInvitarCollection()) {
                Contacto oldIdinvitadoOfInvitarCollectionInvitar = invitarCollectionInvitar.getIdinvitado();
                invitarCollectionInvitar.setIdinvitado(contacto);
                invitarCollectionInvitar = em.merge(invitarCollectionInvitar);
                if (oldIdinvitadoOfInvitarCollectionInvitar != null) {
                    oldIdinvitadoOfInvitarCollectionInvitar.getInvitarCollection().remove(invitarCollectionInvitar);
                    oldIdinvitadoOfInvitarCollectionInvitar = em.merge(oldIdinvitadoOfInvitarCollectionInvitar);
                }
            }
            for (Invitar invitarCollection1Invitar : contacto.getInvitarCollection1()) {
                Contacto oldIdanfitrionOfInvitarCollection1Invitar = invitarCollection1Invitar.getIdanfitrion();
                invitarCollection1Invitar.setIdanfitrion(contacto);
                invitarCollection1Invitar = em.merge(invitarCollection1Invitar);
                if (oldIdanfitrionOfInvitarCollection1Invitar != null) {
                    oldIdanfitrionOfInvitarCollection1Invitar.getInvitarCollection1().remove(invitarCollection1Invitar);
                    oldIdanfitrionOfInvitarCollection1Invitar = em.merge(oldIdanfitrionOfInvitarCollection1Invitar);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Contacto contacto) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Contacto persistentContacto = em.find(Contacto.class, contacto.getId());
            Collection<Nota> notaCollectionOld = persistentContacto.getNotaCollection();
            Collection<Nota> notaCollectionNew = contacto.getNotaCollection();
            Collection<Seramigo> seramigoCollectionOld = persistentContacto.getSeramigoCollection();
            Collection<Seramigo> seramigoCollectionNew = contacto.getSeramigoCollection();
            Collection<Seramigo> seramigoCollection1Old = persistentContacto.getSeramigoCollection1();
            Collection<Seramigo> seramigoCollection1New = contacto.getSeramigoCollection1();
            Collection<Evento> eventoCollectionOld = persistentContacto.getEventoCollection();
            Collection<Evento> eventoCollectionNew = contacto.getEventoCollection();
            Collection<Invitar> invitarCollectionOld = persistentContacto.getInvitarCollection();
            Collection<Invitar> invitarCollectionNew = contacto.getInvitarCollection();
            Collection<Invitar> invitarCollection1Old = persistentContacto.getInvitarCollection1();
            Collection<Invitar> invitarCollection1New = contacto.getInvitarCollection1();
            List<String> illegalOrphanMessages = null;
            for (Nota notaCollectionOldNota : notaCollectionOld) {
                if (!notaCollectionNew.contains(notaCollectionOldNota)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Nota " + notaCollectionOldNota + " since its idcreador field is not nullable.");
                }
            }
            for (Seramigo seramigoCollectionOldSeramigo : seramigoCollectionOld) {
                if (!seramigoCollectionNew.contains(seramigoCollectionOldSeramigo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Seramigo " + seramigoCollectionOldSeramigo + " since its idamigo2 field is not nullable.");
                }
            }
            for (Seramigo seramigoCollection1OldSeramigo : seramigoCollection1Old) {
                if (!seramigoCollection1New.contains(seramigoCollection1OldSeramigo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Seramigo " + seramigoCollection1OldSeramigo + " since its idamigo1 field is not nullable.");
                }
            }
            for (Evento eventoCollectionOldEvento : eventoCollectionOld) {
                if (!eventoCollectionNew.contains(eventoCollectionOldEvento)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Evento " + eventoCollectionOldEvento + " since its idcreador field is not nullable.");
                }
            }
            for (Invitar invitarCollectionOldInvitar : invitarCollectionOld) {
                if (!invitarCollectionNew.contains(invitarCollectionOldInvitar)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Invitar " + invitarCollectionOldInvitar + " since its idinvitado field is not nullable.");
                }
            }
            for (Invitar invitarCollection1OldInvitar : invitarCollection1Old) {
                if (!invitarCollection1New.contains(invitarCollection1OldInvitar)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Invitar " + invitarCollection1OldInvitar + " since its idanfitrion field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Nota> attachedNotaCollectionNew = new ArrayList<Nota>();
            for (Nota notaCollectionNewNotaToAttach : notaCollectionNew) {
                notaCollectionNewNotaToAttach = em.getReference(notaCollectionNewNotaToAttach.getClass(), notaCollectionNewNotaToAttach.getId());
                attachedNotaCollectionNew.add(notaCollectionNewNotaToAttach);
            }
            notaCollectionNew = attachedNotaCollectionNew;
            contacto.setNotaCollection(notaCollectionNew);
            Collection<Seramigo> attachedSeramigoCollectionNew = new ArrayList<Seramigo>();
            for (Seramigo seramigoCollectionNewSeramigoToAttach : seramigoCollectionNew) {
                seramigoCollectionNewSeramigoToAttach = em.getReference(seramigoCollectionNewSeramigoToAttach.getClass(), seramigoCollectionNewSeramigoToAttach.getId());
                attachedSeramigoCollectionNew.add(seramigoCollectionNewSeramigoToAttach);
            }
            seramigoCollectionNew = attachedSeramigoCollectionNew;
            contacto.setSeramigoCollection(seramigoCollectionNew);
            Collection<Seramigo> attachedSeramigoCollection1New = new ArrayList<Seramigo>();
            for (Seramigo seramigoCollection1NewSeramigoToAttach : seramigoCollection1New) {
                seramigoCollection1NewSeramigoToAttach = em.getReference(seramigoCollection1NewSeramigoToAttach.getClass(), seramigoCollection1NewSeramigoToAttach.getId());
                attachedSeramigoCollection1New.add(seramigoCollection1NewSeramigoToAttach);
            }
            seramigoCollection1New = attachedSeramigoCollection1New;
            contacto.setSeramigoCollection1(seramigoCollection1New);
            Collection<Evento> attachedEventoCollectionNew = new ArrayList<Evento>();
            for (Evento eventoCollectionNewEventoToAttach : eventoCollectionNew) {
                eventoCollectionNewEventoToAttach = em.getReference(eventoCollectionNewEventoToAttach.getClass(), eventoCollectionNewEventoToAttach.getId());
                attachedEventoCollectionNew.add(eventoCollectionNewEventoToAttach);
            }
            eventoCollectionNew = attachedEventoCollectionNew;
            contacto.setEventoCollection(eventoCollectionNew);
            Collection<Invitar> attachedInvitarCollectionNew = new ArrayList<Invitar>();
            for (Invitar invitarCollectionNewInvitarToAttach : invitarCollectionNew) {
                invitarCollectionNewInvitarToAttach = em.getReference(invitarCollectionNewInvitarToAttach.getClass(), invitarCollectionNewInvitarToAttach.getId());
                attachedInvitarCollectionNew.add(invitarCollectionNewInvitarToAttach);
            }
            invitarCollectionNew = attachedInvitarCollectionNew;
            contacto.setInvitarCollection(invitarCollectionNew);
            Collection<Invitar> attachedInvitarCollection1New = new ArrayList<Invitar>();
            for (Invitar invitarCollection1NewInvitarToAttach : invitarCollection1New) {
                invitarCollection1NewInvitarToAttach = em.getReference(invitarCollection1NewInvitarToAttach.getClass(), invitarCollection1NewInvitarToAttach.getId());
                attachedInvitarCollection1New.add(invitarCollection1NewInvitarToAttach);
            }
            invitarCollection1New = attachedInvitarCollection1New;
            contacto.setInvitarCollection1(invitarCollection1New);
            contacto = em.merge(contacto);
            for (Nota notaCollectionNewNota : notaCollectionNew) {
                if (!notaCollectionOld.contains(notaCollectionNewNota)) {
                    Contacto oldIdcreadorOfNotaCollectionNewNota = notaCollectionNewNota.getIdcreador();
                    notaCollectionNewNota.setIdcreador(contacto);
                    notaCollectionNewNota = em.merge(notaCollectionNewNota);
                    if (oldIdcreadorOfNotaCollectionNewNota != null && !oldIdcreadorOfNotaCollectionNewNota.equals(contacto)) {
                        oldIdcreadorOfNotaCollectionNewNota.getNotaCollection().remove(notaCollectionNewNota);
                        oldIdcreadorOfNotaCollectionNewNota = em.merge(oldIdcreadorOfNotaCollectionNewNota);
                    }
                }
            }
            for (Seramigo seramigoCollectionNewSeramigo : seramigoCollectionNew) {
                if (!seramigoCollectionOld.contains(seramigoCollectionNewSeramigo)) {
                    Contacto oldIdamigo2OfSeramigoCollectionNewSeramigo = seramigoCollectionNewSeramigo.getIdamigo2();
                    seramigoCollectionNewSeramigo.setIdamigo2(contacto);
                    seramigoCollectionNewSeramigo = em.merge(seramigoCollectionNewSeramigo);
                    if (oldIdamigo2OfSeramigoCollectionNewSeramigo != null && !oldIdamigo2OfSeramigoCollectionNewSeramigo.equals(contacto)) {
                        oldIdamigo2OfSeramigoCollectionNewSeramigo.getSeramigoCollection().remove(seramigoCollectionNewSeramigo);
                        oldIdamigo2OfSeramigoCollectionNewSeramigo = em.merge(oldIdamigo2OfSeramigoCollectionNewSeramigo);
                    }
                }
            }
            for (Seramigo seramigoCollection1NewSeramigo : seramigoCollection1New) {
                if (!seramigoCollection1Old.contains(seramigoCollection1NewSeramigo)) {
                    Contacto oldIdamigo1OfSeramigoCollection1NewSeramigo = seramigoCollection1NewSeramigo.getIdamigo1();
                    seramigoCollection1NewSeramigo.setIdamigo1(contacto);
                    seramigoCollection1NewSeramigo = em.merge(seramigoCollection1NewSeramigo);
                    if (oldIdamigo1OfSeramigoCollection1NewSeramigo != null && !oldIdamigo1OfSeramigoCollection1NewSeramigo.equals(contacto)) {
                        oldIdamigo1OfSeramigoCollection1NewSeramigo.getSeramigoCollection1().remove(seramigoCollection1NewSeramigo);
                        oldIdamigo1OfSeramigoCollection1NewSeramigo = em.merge(oldIdamigo1OfSeramigoCollection1NewSeramigo);
                    }
                }
            }
            for (Evento eventoCollectionNewEvento : eventoCollectionNew) {
                if (!eventoCollectionOld.contains(eventoCollectionNewEvento)) {
                    Contacto oldIdcreadorOfEventoCollectionNewEvento = eventoCollectionNewEvento.getIdcreador();
                    eventoCollectionNewEvento.setIdcreador(contacto);
                    eventoCollectionNewEvento = em.merge(eventoCollectionNewEvento);
                    if (oldIdcreadorOfEventoCollectionNewEvento != null && !oldIdcreadorOfEventoCollectionNewEvento.equals(contacto)) {
                        oldIdcreadorOfEventoCollectionNewEvento.getEventoCollection().remove(eventoCollectionNewEvento);
                        oldIdcreadorOfEventoCollectionNewEvento = em.merge(oldIdcreadorOfEventoCollectionNewEvento);
                    }
                }
            }
            for (Invitar invitarCollectionNewInvitar : invitarCollectionNew) {
                if (!invitarCollectionOld.contains(invitarCollectionNewInvitar)) {
                    Contacto oldIdinvitadoOfInvitarCollectionNewInvitar = invitarCollectionNewInvitar.getIdinvitado();
                    invitarCollectionNewInvitar.setIdinvitado(contacto);
                    invitarCollectionNewInvitar = em.merge(invitarCollectionNewInvitar);
                    if (oldIdinvitadoOfInvitarCollectionNewInvitar != null && !oldIdinvitadoOfInvitarCollectionNewInvitar.equals(contacto)) {
                        oldIdinvitadoOfInvitarCollectionNewInvitar.getInvitarCollection().remove(invitarCollectionNewInvitar);
                        oldIdinvitadoOfInvitarCollectionNewInvitar = em.merge(oldIdinvitadoOfInvitarCollectionNewInvitar);
                    }
                }
            }
            for (Invitar invitarCollection1NewInvitar : invitarCollection1New) {
                if (!invitarCollection1Old.contains(invitarCollection1NewInvitar)) {
                    Contacto oldIdanfitrionOfInvitarCollection1NewInvitar = invitarCollection1NewInvitar.getIdanfitrion();
                    invitarCollection1NewInvitar.setIdanfitrion(contacto);
                    invitarCollection1NewInvitar = em.merge(invitarCollection1NewInvitar);
                    if (oldIdanfitrionOfInvitarCollection1NewInvitar != null && !oldIdanfitrionOfInvitarCollection1NewInvitar.equals(contacto)) {
                        oldIdanfitrionOfInvitarCollection1NewInvitar.getInvitarCollection1().remove(invitarCollection1NewInvitar);
                        oldIdanfitrionOfInvitarCollection1NewInvitar = em.merge(oldIdanfitrionOfInvitarCollection1NewInvitar);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = contacto.getId();
                if (findContacto(id) == null) {
                    throw new NonexistentEntityException("The contacto with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Contacto contacto;
            try {
                contacto = em.getReference(Contacto.class, id);
                contacto.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The contacto with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Nota> notaCollectionOrphanCheck = contacto.getNotaCollection();
            for (Nota notaCollectionOrphanCheckNota : notaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Contacto (" + contacto + ") cannot be destroyed since the Nota " + notaCollectionOrphanCheckNota + " in its notaCollection field has a non-nullable idcreador field.");
            }
            Collection<Seramigo> seramigoCollectionOrphanCheck = contacto.getSeramigoCollection();
            for (Seramigo seramigoCollectionOrphanCheckSeramigo : seramigoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Contacto (" + contacto + ") cannot be destroyed since the Seramigo " + seramigoCollectionOrphanCheckSeramigo + " in its seramigoCollection field has a non-nullable idamigo2 field.");
            }
            Collection<Seramigo> seramigoCollection1OrphanCheck = contacto.getSeramigoCollection1();
            for (Seramigo seramigoCollection1OrphanCheckSeramigo : seramigoCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Contacto (" + contacto + ") cannot be destroyed since the Seramigo " + seramigoCollection1OrphanCheckSeramigo + " in its seramigoCollection1 field has a non-nullable idamigo1 field.");
            }
            Collection<Evento> eventoCollectionOrphanCheck = contacto.getEventoCollection();
            for (Evento eventoCollectionOrphanCheckEvento : eventoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Contacto (" + contacto + ") cannot be destroyed since the Evento " + eventoCollectionOrphanCheckEvento + " in its eventoCollection field has a non-nullable idcreador field.");
            }
            Collection<Invitar> invitarCollectionOrphanCheck = contacto.getInvitarCollection();
            for (Invitar invitarCollectionOrphanCheckInvitar : invitarCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Contacto (" + contacto + ") cannot be destroyed since the Invitar " + invitarCollectionOrphanCheckInvitar + " in its invitarCollection field has a non-nullable idinvitado field.");
            }
            Collection<Invitar> invitarCollection1OrphanCheck = contacto.getInvitarCollection1();
            for (Invitar invitarCollection1OrphanCheckInvitar : invitarCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Contacto (" + contacto + ") cannot be destroyed since the Invitar " + invitarCollection1OrphanCheckInvitar + " in its invitarCollection1 field has a non-nullable idanfitrion field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(contacto);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Contacto> findContactoEntities() {
        return findContactoEntities(true, -1, -1);
    }

    public List<Contacto> findContactoEntities(int maxResults, int firstResult) {
        return findContactoEntities(false, maxResults, firstResult);
    }

    private List<Contacto> findContactoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Contacto.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Contacto findContacto(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Contacto.class, id);
        } finally {
            em.close();
        }
    }

    public int getContactoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Contacto> rt = cq.from(Contacto.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
